/*
 * MIT License
 *
 * Copyright (c) 2018 Justson
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.xy.tianyu.view

import android.app.Activity
import android.content.Context
import android.os.Build
import android.util.AttributeSet
import com.coolindicator.sdk.CoolIndicator
import com.coolindicator.sdk.R
import com.just.agentweb.AgentWebUtils
import com.just.agentweb.BaseIndicatorView
import com.xy.tianyu.view.CoolIndicatorLayout

/**
 * @author cenxiaozhong
 * @date 2018/2/23
 * @since 1.0.0
 */
class CoolIndicatorLayout @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = -1
) : BaseIndicatorView(context, attrs, defStyleAttr) {
    var mCoolIndicator: CoolIndicator? = null
    override fun show() {
        this.visibility = VISIBLE
        mCoolIndicator!!.start()
    }

    override fun setProgress(newProgress: Int) {}
    override fun hide() {
        mCoolIndicator!!.complete()
    }

    override fun offerLayoutParams(): LayoutParams {
        return LayoutParams(-1, AgentWebUtils.dp2px(context, 3f))
    }

    companion object {
        private val TAG = CoolIndicatorLayout::class.java.simpleName
    }

    init {
        mCoolIndicator = CoolIndicator.create(context as Activity)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mCoolIndicator?.progressDrawable = context.getResources()
                .getDrawable(R.drawable.default_drawable_indicator, context.getTheme())
        } else {
            mCoolIndicator?.progressDrawable = context.getResources().getDrawable(R.drawable.default_drawable_indicator)
        }
        this.addView(mCoolIndicator, offerLayoutParams())
    }
}